﻿namespace AnyOffice.WebControls
{
    using System;
    using System.ComponentModel;
    using System.Data;
    using System.Drawing;
    using System.Web.UI;
    using System.Web.UI.WebControls;

    [DefaultProperty("Text"), ToolboxData("<{0}:HFGridView runat=server></{0}:HFGridView>")]
    public class HFGridView : GridView
    {
        public bool AddBindCheckBoxColumn(string headName, string field, string trueValue, double colWidth)
        {
            try
            {
                TemplateField field2 = new TemplateField();
                field2.ItemTemplate = new BindCheckBoxColumn(field, trueValue);
                field2.HeaderText = headName;
                field2.HeaderStyle.Width = Unit.Percentage(colWidth);
                field2.HeaderStyle.Wrap = true;
                field2.ItemStyle.Width = Unit.Percentage(colWidth);
                field2.ItemStyle.Wrap = true;
                field2.ControlStyle.Width = Unit.Percentage(100.0);
                field2.ControlStyle.Height = Unit.Percentage(100.0);
                this.Columns.Add(field2);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddButtonColumn(string headName, string displayText, string commandName, double colWidth)
        {
            try
            {
                ButtonField field = new ButtonField();
                field.ButtonType = ButtonType.Button;
                field.CommandName = commandName;
                field.HeaderText = headName;
                field.Text = displayText;
                field.HeaderStyle.Width = Unit.Percentage(colWidth);
                field.HeaderStyle.Wrap = true;
                field.ItemStyle.Width = Unit.Percentage(colWidth);
                field.ItemStyle.Wrap = true;
                field.ControlStyle.Width = Unit.Percentage(100.0);
                field.ControlStyle.Height = Unit.Percentage(100.0);
                field.ControlStyle.CssClass = "gbutton";
                this.Columns.Add(field);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddCheckBoxColumn(string headName, double colWidth)
        {
            try
            {
                TemplateField field = new TemplateField();
                field.ItemTemplate = new CheckBoxColumn();
                field.HeaderText = headName;
                field.HeaderStyle.Width = Unit.Percentage(colWidth);
                field.HeaderStyle.Wrap = true;
                field.ItemStyle.Width = Unit.Percentage(colWidth);
                field.ItemStyle.Wrap = true;
                field.ControlStyle.Width = Unit.Percentage(100.0);
                field.ControlStyle.Height = Unit.Percentage(100.0);
                this.Columns.Add(field);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddCol(string headName, string fieldName, double colWidth)
        {
            return this.AddCol(headName, fieldName, colWidth, true);
        }

        public bool AddCol(string headName, string fieldName, double colWidth, bool visible)
        {
            try
            {
                BoundField field = new BoundField();
                field.DataField = fieldName;
                field.HeaderText = headName;
                field.HeaderStyle.Width = Unit.Percentage(colWidth);
                field.HeaderStyle.Wrap = true;
                field.Visible = visible;
                field.ItemStyle.Width = Unit.Percentage(colWidth);
                field.ItemStyle.Wrap = true;
                field.ControlStyle.Width = Unit.Percentage(100.0);
                field.ControlStyle.Height = Unit.Percentage(100.0);
                this.Columns.Add(field);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddCols(string[] headNames, string[] fieldNames, double[] colWidths)
        {
            bool[] visibles = new bool[headNames.Length];
            for (int i = 0; i < visibles.Length; i++)
            {
                visibles[i] = true;
            }
            return this.AddCols(headNames, fieldNames, colWidths, visibles);
        }

        public bool AddCols(string[] headNames, string[] fieldNames, double[] colWidths, bool[] visibles)
        {
            try
            {
                for (int i = 0; i < headNames.Length; i++)
                {
                    BoundField field = new BoundField();
                    field.DataField = fieldNames[i];
                    field.HeaderText = headNames[i];
                    field.HeaderStyle.Width = Unit.Percentage(colWidths[i]);
                    field.HeaderStyle.Wrap = true;
                    field.Visible = visibles[i];
                    field.ItemStyle.Width = Unit.Percentage(colWidths[i]);
                    field.ItemStyle.Wrap = true;
                    this.Columns.Add(field);
                }
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddDropDownListColumn(string headName, string[] displayText, string[] dataValue, double colWidth, string selectedField)
        {
            try
            {
                TemplateField field = new TemplateField();
                field.ItemTemplate = new DropDownListColumnArr(displayText, dataValue, selectedField);
                field.HeaderText = headName;
                field.HeaderStyle.Width = Unit.Percentage(colWidth);
                field.HeaderStyle.Wrap = true;
                field.ItemStyle.Width = Unit.Percentage(colWidth);
                field.ItemStyle.Wrap = true;
                field.ControlStyle.Width = Unit.Percentage(100.0);
                field.ControlStyle.Height = Unit.Percentage(100.0);
                this.Columns.Add(field);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddDropDownListColumn(string headName, DataView dataView, string displayText, string dataValue, double colWidth, string selectedField)
        {
            try
            {
                TemplateField field = new TemplateField();
                field.ItemTemplate = new DropDownListColumn(displayText, dataValue, dataView, selectedField);
                field.HeaderText = headName;
                field.HeaderStyle.Width = Unit.Percentage(colWidth);
                field.HeaderStyle.Wrap = true;
                field.ItemStyle.Width = Unit.Percentage(colWidth);
                field.ItemStyle.Wrap = true;
                field.ControlStyle.Width = Unit.Percentage(100.0);
                field.ControlStyle.Height = Unit.Percentage(100.0);
                this.Columns.Add(field);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddHyperLinkColumn(string headName, string displayText, string navUrl, string bindField, double colWidth)
        {
            try
            {
                HyperLinkField field = new HyperLinkField();
                field.NavigateUrl = navUrl;
                field.DataTextField = displayText;
                field.DataNavigateUrlFields = new string[] { bindField };
                field.DataNavigateUrlFormatString = navUrl;
                field.Target = "";
                field.HeaderText = headName;
                field.HeaderStyle.Width = Unit.Percentage(colWidth);
                field.HeaderStyle.Wrap = true;
                field.ItemStyle.Width = Unit.Percentage(colWidth);
                field.ItemStyle.Wrap = true;
                field.ControlStyle.Width = Unit.Percentage(100.0);
                field.ControlStyle.Height = Unit.Percentage(100.0);
                this.Columns.Add(field);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool AddTextBoxColumn(string headName, string fieldName, double colWidth)
        {
            try
            {
                TemplateField field = new TemplateField();
                field.ItemTemplate = new CTemplateColumn(fieldName, TCType.TextBox);
                field.HeaderText = headName;
                field.HeaderStyle.Width = Unit.Percentage(colWidth);
                field.HeaderStyle.Wrap = true;
                field.ItemStyle.Width = Unit.Percentage(colWidth);
                field.ItemStyle.Wrap = true;
                field.ControlStyle.CssClass = "dibg";
                this.Columns.Add(field);
                return true;
            }
            catch
            {
                return false;
            }
        }

        public bool BindGrid(DataView dataView)
        {
            try
            {
                this.AutoGenerateColumns = false;
                this.DataSource = dataView;
                this.DataBind();
                return true;
            }
            catch (Exception exception)
            {
                string message = exception.Message;
                return false;
            }
        }

        public bool BindGrid(DataView dataView, string[] headNames, string[] fieldNames, double[] colWidths)
        {
            try
            {
                this.Columns.Clear();
                this.AutoGenerateColumns = false;
                this.AddCols(headNames, fieldNames, colWidths);
                this.DataSource = dataView;
                this.DataBind();
                return true;
            }
            catch (Exception exception)
            {
                string message = exception.Message;
                return false;
            }
        }

        public bool BindGrid(DataView dataView, string[] headNames, string[] fieldNames, double[] colWidths, bool[] visibles)
        {
            try
            {
                this.Columns.Clear();
                this.AutoGenerateColumns = false;
                this.AddCols(headNames, fieldNames, colWidths, visibles);
                this.DataSource = dataView;
                this.DataBind();
                return true;
            }
            catch (Exception exception)
            {
                string message = exception.Message;
                return false;
            }
        }

        public string GetCellValue(int rowIndex, int colIndex)
        {
            if (this.Rows[rowIndex].Cells[colIndex].Controls.Count > 0)
            {
                switch (this.Rows[rowIndex].Cells[colIndex].Controls[0].GetType().ToString())
                {
                    case "System.Web.UI.LiteralControl":
                        return ((LiteralControl) this.Rows[rowIndex].Cells[colIndex].Controls[0]).Text;

                    case "System.Web.UI.WebControls.TextBox":
                        return ((TextBox) this.Rows[rowIndex].Cells[colIndex].Controls[0]).Text;

                    case "System.Web.UI.WebControls.Button":
                        return ((Button) this.Rows[rowIndex].Cells[colIndex].Controls[0]).Text;

                    case "System.Web.UI.WebControls.DropDownList":
                        return ((DropDownList) this.Rows[rowIndex].Cells[colIndex].Controls[0]).SelectedValue;

                    case "System.Web.UI.WebControls.HyperLink":
                        return ((HyperLink) this.Rows[rowIndex].Cells[colIndex].Controls[0]).NavigateUrl;

                    case "System.Web.UI.WebControls.CheckBox":
                        if (!((CheckBox) this.Rows[rowIndex].Cells[colIndex].Controls[0]).Checked)
                        {
                            return "0";
                        }
                        return "1";
                }
            }
            return this.Rows[rowIndex].Cells[colIndex].Text;
        }

        protected override void OnInit(EventArgs e)
        {
            base.AlternatingRowStyle.CssClass = "gRowAlternate";
            base.HeaderStyle.CssClass = "gHeader";
            base.SelectedRowStyle.CssClass = "gSelectedRow";
            base.RowStyle.CssClass = "gSelectedRow";
            base.PagerStyle.CssClass = "gHeader";
            base.EditRowStyle.CssClass = "gHeader";
            base.EmptyDataRowStyle.CssClass = "gHeader";
            this.BorderColor = Color.FromArgb(0x9d, 0xb6, 0xdf);
            this.BorderWidth = Unit.Pixel(1);
        }

        protected override void OnRowDataBound(GridViewRowEventArgs e)
        {
            base.OnRowDataBound(e);
            if (e.Row.RowIndex != -1)
            {
                e.Row.Attributes.Add("ondblclick", "tog(this,this.style.background)");
            }
        }

        public void SetButtonStyle()
        {
            for (int i = 0; i < this.Columns.Count; i++)
            {
                try
                {
                    ButtonField field = (ButtonField) this.Columns[i];
                    if (field.ButtonType == ButtonType.Button)
                    {
                        field.ControlStyle.CssClass = "gbutton";
                        field.ControlStyle.Width = Unit.Percentage(100.0);
                        field.ControlStyle.Height = Unit.Percentage(100.0);
                    }
                }
                catch
                {
                }
            }
        }
    }
}

